package com.wildlife.springboot.mapper.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

@Service
public class AudioUploadService {
    private static final String AUDIO_UPLOAD_DIR = "D:/Program Files (x86)/IdeaProjects/Wildlife/sounds/";

    public String uploadAudio(MultipartFile file) throws IOException {
        // 获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null) {
            throw new IOException("文件名不能为空");
        }

        // 生成一个唯一的文件名（避免文件覆盖）
        String uniqueFileName = UUID.randomUUID().toString() + "_" + originalFilename;
        Path targetPath = Paths.get(AUDIO_UPLOAD_DIR, uniqueFileName);

        // 创建目标文件
        Files.copy(file.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);

        // 返回相对路径（供前端访问）
        return "/sounds/" + uniqueFileName;
    }
}
